package org.atovio.coolexam.controller;

import org.atovio.coolexam.common.Result;
import org.atovio.coolexam.entity.Subject;
import org.atovio.coolexam.service.SubjectService;
import org.atovio.coolexam.service.UserService;
import org.atovio.coolexam.viewmodel.admin.subject.TableRowVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/subjects")
public class SubjectController {

    private final SubjectService subjectService;
    private final UserService userService;

    @Autowired
    public SubjectController(SubjectService subjectService, UserService userService) {
        this.subjectService = subjectService;
        this.userService = userService;
    }

    @GetMapping("/criteria")
    public Result query(@RequestParam String name) {
        List<TableRowVM> tableData = new ArrayList<>();

        List<Subject> subjects = subjectService.getLikeUserName(name);
        subjects.forEach(s -> {
            TableRowVM vm = new TableRowVM();
            vm.setSubject(s);
            vm.setUser(userService.getByUserId(s.getTUserId()));
            tableData.add(vm);
        });
        return Result.success(tableData);
    }

    @PostMapping
    public Result addSubject(@RequestBody Map<String, Object> map) {
        Integer id = (Integer) map.get("id");
        String name = (String) map.get("name");
        Integer tUserId = (Integer) map.get("tUserId");
        Subject subject = new Subject(id, name, tUserId);
        System.out.println(subject);
        subjectService.addSubject(subject);
        return Result.success();
    }

    @PutMapping
    public Result modify(@RequestBody Map<String, Object> map) {
        Integer id = (Integer) map.get("id");
        String name = (String) map.get("name");
        Integer tUserId = (Integer) map.get("tUserId");
        Subject subject = new Subject(id, name, tUserId);
        return Result.success(subjectService.update(subject));
    }

    /*
     * 获取数据库中学科数据和其对应的任课教师数据
     * @return 包含了数据库中学科数据和其对应的任课教师数据的响应结果
     */
    @GetMapping
    public Result getSubjectList() {
        // 创建一个空的泛型类型参数为TableRowVM的链表，用以装载视图模型实体类
        List<TableRowVM> tableData = new ArrayList<>();

        // 调用服务层，获取持久层数据库中所有的学科记录
        List<Subject> subjects = subjectService.getAllSubject();
        // 遍历这些学科
        subjects.forEach(s -> {
            // 创建一个空的列表行视图模型对象，用以装载学科信息和任课教师信息
            TableRowVM vm = new TableRowVM();
            // 装载数据
            vm.setSubject(s);
            vm.setUser(userService.getByUserId(s.getTUserId()));
            // 收集列表行视图模型
            tableData.add(vm);
        });
        // 返回收集到的列表行数据模型
        return Result.success(tableData);
    }

    @DeleteMapping("/{id}")
    public Result removeById(@PathVariable Integer id) {
        subjectService.deleteById(id);
        return Result.success();
    }
}
